package com.fick.hibernate.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.fick.hibernate.model.Admin;

/**
 * A data access object (DAO) providing persistence and search support for Admin
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.fick.hibernate.model.Admin
 * @author MyEclipse Persistence Tools
 */

public class AdminDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(AdminDAO.class);

	// property constants
	public static final String ADMIN_NAME = "adminName";

	public static final String ADMIN_PASS = "adminPass";

	public static final String FULL_NAME = "fullName";

	public static final String QUESTION = "question";

	public static final String ANSWER = "answer";

	public static final String IP = "ip";

	public static final String ENTER_NUM = "enterNum";

	public void attachClean(Admin instance) {
		log.debug("attaching clean Admin instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachDirty(Admin instance) {
		log.debug("attaching dirty Admin instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Admin instances");
		try {
			String queryString = "from Admin";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List<Admin> findAllByPage(Integer currPage, Integer pageSize) {
		log.debug("find all admin by page  currPage is" + currPage + " pageSize is " + pageSize);
		try {
			String queryString = "from Admin";
			Query query = getSession().createQuery(queryString);
			query.setFirstResult((currPage - 1) * pageSize);
			query.setMaxResults(pageSize);
			List<Admin> li = query.list();
			return li;
		} catch (RuntimeException re) {
			log.error("find all admin by page failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findByAdminName(Object adminName) {
		return findByProperty(ADMIN_NAME, adminName);
	}

	public List findByAdminPass(Object adminPass) {
		return findByProperty(ADMIN_PASS, adminPass);
	}

	public List findByAnswer(Object answer) {
		return findByProperty(ANSWER, answer);
	}

	public List findByEnterNum(Object enterNum) {
		return findByProperty(ENTER_NUM, enterNum);
	}

	public List findByExample(Admin instance) {
		log.debug("finding Admin instance by example");
		try {
			List results = getSession().createCriteria("com.fick.hibernate.model.Admin").add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findByFullName(Object fullName) {
		return findByProperty(FULL_NAME, fullName);
	}

	public List<Admin> findByHql(String hql) {
		log.debug("finding by hql" + hql);
		try {
			Query query = getSession().createQuery(hql);
			List<Admin> li = query.list();
			return li;
		} catch (RuntimeException re) {
			log.error("find by hql failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List<Admin> findByHqlPage(String hql, Integer currPage, Integer pageSize) {
		log.debug("find page by Hql hql is " + hql + " currPage is " + currPage + " pagesize is " + pageSize);
		try {
			Query query = getSession().createQuery(hql);
			query.setFirstResult((currPage - 1) * pageSize);
			query.setMaxResults(currPage * pageSize);
			List<Admin> li = query.list();
			return li;
		} catch (RuntimeException re) {
			log.error("find page by hql is failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public Admin findById(java.lang.Integer id) {
		log.debug("getting Admin instance with id: " + id);
		try {
			Admin instance = (Admin) getSession().get("com.fick.hibernate.model.Admin", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findByIp(Object ip) {
		return findByProperty(IP, ip);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Admin instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Admin as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findByQuestion(Object question) {
		return findByProperty(QUESTION, question);
	}

	public Admin merge(Admin detachedInstance) {
		log.debug("merging Admin instance");
		try {
			Admin result = (Admin) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
}